package com.banmao.CarlCamp.Day09;

/**
 * https://leetcode.cn/problems/reverse-linked-list/submissions/
 */
public class LC206 {

    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // 普通的方法
        // 虚拟头
        ListNode dummyHead = new ListNode(-1, head);

        ListNode pre = dummyHead;
        ListNode cur = head;
        // ListNode next = head.next;

        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = (pre == dummyHead ? null : pre);
            pre = cur;
            cur = temp;
        }

        return pre;
    }

    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

}
